package hello.service;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import hello.model.CounterOfUserEntry;
import hello.model.User;
import hello.model.UserComment;
import hello.model.UserEntry;

import org.slim3.datastore.Datastore;
import org.slim3.datastore.EntityNotFoundRuntimeException;
import org.slim3.datastore.GlobalTransaction;

import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Transaction;


public class BakaService {
    private static BakaService singleton = new BakaService();
    
    private BakaService(){}
    
    public static BakaService getInstance(){
        return singleton;
    }
    
    public final static String SEPARATOR = "__sep__";
    
    public boolean verifyRequestParameter(HttpServletRequest request, List<String> params){
        for(String s : params){
            Object param = request.getParameter(s);
            if(param == null){
                return false;
            }
        }
        return true;
    }
    
    public void entry(String userId, String entryId, String destination, String title) throws Exception{
        GlobalTransaction gtx = Datastore.beginGlobalTransaction();
        // カウンタを取得
        CounterOfUserEntry counter = gtx.get(CounterOfUserEntry.class, CounterOfUserEntry.createKey());
        
        Key key = Datastore.createKey(UserEntry.class, entryId);
        try{
            // 既にレコードが存在していたら何もせずに戻る
            UserEntry isEntry = gtx.get(UserEntry.class, key);
            return;
        }catch(EntityNotFoundRuntimeException ex){
            // 何もしない
        }
        
        // エントリー情報生成
        Date now = new Date();
        UserEntry userEntry = new UserEntry();
        userEntry.setKey(key);
        userEntry.setUserId(userId);
        userEntry.setDestination(destination);
        userEntry.setTitle(title);
        userEntry.setCreated(now);
        userEntry.setThumbnailLoaded(false);
        userEntry.setThumbnailUrl("");
        userEntry.setCounter(counter.getCounter());
        gtx.put(userEntry);
        
        // カウンタをインクリメント
        counter.increment();
        gtx.put(counter);
        
        gtx.commit();
    }
    
    public void setUser(String userId, String userName, String profileImageUrl){
        Key key = Datastore.createKey(User.class, userId);
        User user = new User();
        user.setKey(key);
        user.setName(userName);
        user.setProfileImageUrl(profileImageUrl);
        Datastore.put(user);
    }
    
    public void delete(String userId, String entryId){
        Key key = Datastore.createKey(UserEntry.class, userId + BakaService.SEPARATOR + entryId);
        Datastore.delete(key);        
    }
    
    public void addComment(String userId, String entryId, String comment){
        Key key = Datastore.createKey(UserComment.class, userId + BakaService.SEPARATOR + entryId);
        Date now = new Date();
        UserComment userComment = new UserComment();
        userComment.setKey(key);
        userComment.setComment(comment);
        userComment.setCreated(now);
        Datastore.put(userComment);
    }

    public void removeComment(String userId, String entryId){
        Key key = Datastore.createKey(UserComment.class, userId + BakaService.SEPARATOR + entryId);
        Datastore.delete(key);
    }
}
